package com.lahuan.service.impl;

import com.lahuan.config.datasouce.XaTransaction;
import com.lahuan.service.TestService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lfy
 */
@Component
public class TestServiceImpl implements TestService {
    @Resource(name="jdbcTemplate1")
    JdbcTemplate jdbcTemplate1;
    @Resource(name="jdbcTemplate2")
    JdbcTemplate jdbcTemplate2;

    @XaTransaction
    @Override
    public void testSame() {
        System.out.println("testSame()");
        jdbcTemplate1.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, 'testSuccess');");
        System.out.println("执行完第1条SQL");
        jdbcTemplate1.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, 'testSuccess');");
        System.out.println("执行完第2条SQL");
    }

    @XaTransaction
    @Override
    public void testSuccess() {
        System.out.println("进入testSuccess()");
        jdbcTemplate1.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, 'testSuccess');");
        System.out.println("执行完第1条SQL");
        jdbcTemplate2.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, 'testSuccess');");
        System.out.println("执行完第2条SQL");

    }

    @XaTransaction
    @Override
    public void testError() {
        System.out.println("进入testError()");
        jdbcTemplate1.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, 'testError');");
        System.out.println("执行完第1条SQL");
        //传一个超出长度限制的sql
        jdbcTemplate2.execute("INSERT INTO `t_order`( `user_id`, `order_sn`) VALUES (1, " +
                "'TestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestError" +
                "TestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestError" +
                "TestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestErrorTestError');");
        System.out.println("执行完第2条SQL");
    }
}
